package com.pl.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pl.entity.WmStorageArea;
import com.pl.entity.WmStorageLocation;
import com.pl.lister.WmStorageAreaReadLister;
import com.pl.service.WmStorageAreaService;
import com.pl.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 库位表(WmStorageArea)表控制层
 *
 * @author makejava
 * @since 2024-04-28 15:00:09
 */
@RestController
@RequestMapping("/api/wmWarehouseArea")
public class WmStorageAreaController {
    /**
     * 服务对象
     */
    @Autowired
    private WmStorageAreaService wmStorageAreaService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<WmStorageArea> wmStorageAreas = wmStorageAreaService.queryByPage(map);
        if (wmStorageAreas != null) {
            PageInfo<WmStorageArea> wmStorageLocationPageInfo = new PageInfo<>(wmStorageAreas);
            return R.ok(wmStorageLocationPageInfo);
        }
        return R.fail("查询失败");


    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.wmStorageAreaService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param wmStorageArea 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(WmStorageArea wmStorageArea, HttpSession session) {
        boolean result = this.wmStorageAreaService.insert(wmStorageArea, session);
        if (result){
            return R.ok();
        }
        return R.fail("插入失败");
    }

    /**
     * 编辑数据
     *
     * @param wmStorageArea 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(WmStorageArea wmStorageArea) {
       boolean result = this.wmStorageAreaService.update(wmStorageArea);
       if(result){
           return R.ok();
       }
       return R.fail("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Long id) {
        boolean b = this.wmStorageAreaService.deleteById(id);
        if (b){
            return R.ok();
        }
        return R.fail("删除失败");
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list){
        boolean result=wmStorageAreaService.deleteBatch(list);
        if (result){
            return R.ok();
        }
        return R.fail("删除失败！");
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @RequestMapping("/importAreaFile")
    public String importArea(@Param("file") MultipartFile file){
       return wmStorageAreaService.importArea(file);
    }

    /**
     * 导出
     * @param response
     */
    @RequestMapping("/exportAreaFile")
    public void exportArea(HttpServletResponse response){
        wmStorageAreaService.exportArea(response);
    }
}

